package com.crayon.account.fragmentspop;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.crayon.account.dao.DAOCardBank;
import com.crayon.account.dao.DAOCategory;
import com.crayon.account.data.ItemList;
import com.crayon.sharemenu.R;
import com.crayon.util.UIBIND;

abstract public class FragmentPopEditRoot extends FragmentPopRoot implements OnClickListener{

	abstract public void OnEventNewCreat();
	abstract public void OnEventDelete();
	abstract public boolean OnEventUpdate(String cate);
	ItemList itemList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_FRAME,android.R.style.Theme_Translucent_NoTitleBar);
		itemList = (ItemList)getArguments().getSerializable("item");
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_dialog_editcate, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		UIBIND.bindView(getView(), R.id.buttonpop_new, this);
		UIBIND.bindView(getView(), R.id.buttonpop_cancel, this);
		UIBIND.bindView(getView(), R.id.buttonpop_save, this);
		UIBIND.bindView(getView(), R.id.buttonpop_delete, this);
		//안보이게 할 아이콘 선택
		UIBIND.bindViewVisible(getView(), R.id.buttonpop_new, false);

		
		UIBIND.bindText(getView(), R.id.textEdit, itemList.getSummary());

		
		if(itemList!=null ){
			if(itemList.getTag()!=null){
				Object obj = itemList.getTag();
				if(obj instanceof DAOCardBank){
					DAOCardBank bank = (DAOCardBank)obj;
					if(bank.getType()==DAOCardBank.BANK){
						UIBIND.bindText(getView(), R.id.textPopTitle, getString(R.string.main_list_menutext_listbanks));
					}else if(bank.getType()==DAOCardBank.CARD){
						UIBIND.bindText(getView(), R.id.textPopTitle, getString(R.string.main_list_menutext_listcards));
					}
				}else if(obj instanceof DAOCategory){
					DAOCategory cate = (DAOCategory)obj;
					if(cate.getType()==DAOCategory.INCOM){
						UIBIND.bindText(getView(), R.id.textPopTitle, getString(R.string.main_list_menutext_listcatein));
					}else if(cate.getType()==DAOCategory.OUTCOM){
						UIBIND.bindText(getView(), R.id.textPopTitle, getString(R.string.main_list_menutext_listcateout));
					}
					
				}

			}
			//			UIBIND.bindView(getView(), R.id.buttonpop_new, this);
			if(itemList.getId()<=0){
				initNewCreate();
			}
		}
		
		
		
		super.onActivityCreated(savedInstanceState);
	}

	private void initNewCreate(){
		OnEventNewCreat();
		UIBIND.bindText(getView(), R.id.textEdit, "");
		UIBIND.bindViewVisible(getView(), R.id.buttonpop_new, false);
		UIBIND.bindViewVisible(getView(), R.id.buttonpop_delete, false);

	}
	public ItemList getItemList() {
		return itemList;
	}

	public void setItemList(ItemList itemList) {
		this.itemList = itemList;
	}

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.buttonpop_new:
			initNewCreate();
			
			break;
		case R.id.buttonpop_cancel:
			
			close();
			break;
		case R.id.buttonpop_delete:
			OnEventDelete();
			close();
			break;
		case R.id.buttonpop_save:
			if(OnEventUpdate(UIBIND.getTextValue(getView(), R.id.textEdit))){
				close();
			}
			break;
		}
	}
	
	private void close(){
		dismiss();
		if(getOnDialogFragment()!=null) getOnDialogFragment().onDismissDialog(this);

	}


}
